package com.feifan.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.feifan.api.annotation.LogType;
import com.feifan.api.annotation.SystemLog;
import com.feifan.api.annotation.UserLoginToken;
import com.feifan.api.dto.OmsMoneyInfoParam;
import com.feifan.api.dto.OmsOrderDeliveryParam;
import com.feifan.api.dto.OmsOrderQueryParam;
import com.feifan.api.dto.OmsReceiverInfoParam;
import com.feifan.api.model.OmsOrder;
import com.feifan.api.model.OmsOrderSetting;
import com.feifan.api.service.IOmsOrderService;
import com.feifan.api.service.IOmsOrderSettingService;
import com.feifan.common.CommonPage;
import com.feifan.common.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-09 13:07
 * @description: Functions of this class is
 **/
@RestController
@CrossOrigin
@RequestMapping(value = "/order")
@Api(tags = "OmsOrderController",description= "订单管理")
public class OmsOrderController {
    //dubbo做法
    @Reference(
            version ="1.0.0",
            interfaceName = "com.feifan.api.service.IOmsOrderService",
            interfaceClass = IOmsOrderService.class,
            timeout = 170000
    )
    private IOmsOrderService service;


    @ApiOperation(value = "查询订单")
    @ResponseBody
    @GetMapping(value = "/list")
    @UserLoginToken
    @SystemLog(description ="查询订单",type = LogType.ORDER_LIST)
    public CommonResult<CommonPage<OmsOrder>> list( OmsOrderQueryParam queryParam,
                                                   @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                   @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
       return CommonResult.success(service.list(queryParam,pageSize,pageNum));

    }

    @ApiOperation(value = "获得订单详情、订单信息、商品信息、操作记录")
    @ResponseBody
    @GetMapping(value = "/{id}")
    @UserLoginToken
    @SystemLog(description ="获得订单详情、订单信息、商品信息、操作记录",type = LogType.ORDER_DETAIL)
    public CommonResult<OmsOrder> detail(@PathVariable Long id){
        return CommonResult.success(service.detail(id));
    }


    @ApiOperation(value = "备注订单")
    @ResponseBody
    @PostMapping(value = "/update/note")
    @UserLoginToken
    @SystemLog(description ="备注订单",type = LogType.ORDER_NOTE_UPDATE)
    public CommonResult updateNode(@RequestParam(value = "id") Long id,
                                   @RequestParam(value = "note")String note,
                                   @RequestParam(value = "status")Integer status){
        CommonResult commonResult;
        try {
            service.updateNote(id,note,status);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @ApiOperation(value = "批量删除订单")
    @ResponseBody
    @PostMapping(value = "/delete")
    @UserLoginToken
    @SystemLog(description ="批量删除订单",type = LogType.ORDER_DELETE)
    public CommonResult delete(@RequestParam(value = "ids")List<Long> ids){
        CommonResult commonResult;
        try {
            ids.stream().forEach(id->service.delete(id));
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }
    @ApiOperation(value = "批量关闭订单")
    @ResponseBody
    @PostMapping(value = "/update/close")
    @UserLoginToken
    @SystemLog(description ="批量关闭订单",type = LogType.ORDER_CLOE_UPDATE)
    public CommonResult close(@RequestParam(value = "ids")List<Long> ids,@RequestParam(value = "node") String note){
        CommonResult commonResult;
        try {
            ids.stream().forEach(id->service.close(id,note));
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @ApiOperation(value = "批量发货")
    @ResponseBody
    @PostMapping(value = "/update/delivery")
    @UserLoginToken
    @SystemLog(description ="批量发货",type = LogType.ORDER_DELIVERY)
    public CommonResult delivery(@RequestBody List<OmsOrderDeliveryParam> params){
        CommonResult commonResult;
        try {
            params.stream().forEach(param->service.delivery(param));
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }


    @ApiOperation(value = "修改收货人消息")
    @ResponseBody
    @PostMapping(value = "/update/receiverInfo")
    @UserLoginToken
    public CommonResult updateReceiverInfo(@RequestBody OmsReceiverInfoParam param){
        CommonResult commonResult;
        try {
            service.updateReceiverInfo(param);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @ApiOperation(value = "修改订单费用信息")
    @ResponseBody
    @PostMapping(value = "/update/moneyInfo")
    @UserLoginToken
    public CommonResult updateMoneyInfo(@RequestBody OmsMoneyInfoParam param){
        CommonResult commonResult;
        try {
            service.updateMoneyInfo(param);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @ApiOperation(value = "取消单个超时订单")
    @ResponseBody
    @PostMapping(value = "/cancelOrder")
    @UserLoginToken
    @SystemLog(description ="取消单个超时订单",type = LogType.ORDER_CANCEL)
    public CommonResult cancelOrder(@RequestParam(value = "ids")List<Long>ids,@RequestParam(value = "minute")Integer minute){
        //在演示之前，先把OmsOrder中的status改成0，这样的话就可以演示自动取消订单了
//        service.sendDelayMessageCancelOrder(ids.get(0),minute);//取出要取消的的第一个订单进行处理
        ids.stream().forEach(id-> service.sendDelayMessageCancelOrder(id,minute));//对每一个订单进行取消操作
        return CommonResult.success(true);
    }

}